package abg.core.common.util;

import org.springframework.util.ResourceUtils;

import java.io.*;
import java.math.BigInteger;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.security.MessageDigest;


public class FileKit {

    //
    public static File getClassPath(String filePath) {
        try {
            return ResourceUtils.getFile("classpath:" + filePath);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 存储文件到本地
     *
     * @param fileURL  文件的URL "https://example.com/example.jpg"
     * @param savePath 保存文件的路径和文件名 "/aaa/bbb/example.jpg" "E:/aaa/bbb/example.jpg"
     * @return true/false
     */
    public static boolean saveLocal(String fileURL, String savePath) {
        return saveLocal(fileURL, savePath, 1024 * 10);
    }

    public static boolean saveLocal(String fileURL, String savePath, int readSize) {
        try {
            HttpClient client = HttpClient.newHttpClient();

            HttpRequest request = HttpRequest.newBuilder().uri(URI.create(fileURL)).build();

            HttpResponse<InputStream> response = client.send(request, HttpResponse.BodyHandlers.ofInputStream());

            return saveLocal(response.body(), savePath, readSize);

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 存储文件到本地
     *
     * @param inputStream 文件输入流
     * @param savePath    保存文件的路径和文件名 "/aaa/bbb/example.jpg" "E:/aaa/bbb/example.jpg"
     * @return true/false
     */
    public static boolean saveLocal(InputStream inputStream, String savePath, int readSize) {
        try (FileOutputStream outputStream = new FileOutputStream(savePath)) {

            byte[] buffer = new byte[readSize];
            int bytesRead;

            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }

            outputStream.close();
            inputStream.close();

            System.out.println("文件保存成功。");

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 存储文件到本地
     *
     * @param bytes    全部字节
     * @param savePath 保存文件的路径和文件名 "/aaa/bbb/example.jpg" "E:/aaa/bbb/example.jpg"
     * @return 文件路径
     */
    public static String saveLocal(byte[] bytes, String savePath) {
        return saveLocal(bytes, new File(savePath));
    }

    public static String saveLocal(byte[] bytes, File file) {
        try (FileOutputStream outputStream = new FileOutputStream(file)) {
            outputStream.write(bytes);
            outputStream.close();
            System.out.println("文件保存成功。");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return file.getAbsolutePath();
    }

    //
    public static String fileContent(File file) {
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));) {
            String content;
            StringBuilder sb = new StringBuilder();
            while ((content = reader.readLine()) != null) {
                sb.append(content);
            }
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String getMD5(InputStream fis) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] buffer = new byte[4096];
            int n = 0;
            while ((n = fis.read(buffer)) != -1) {
                md.update(buffer, 0, n);
            }
            // 完成哈希计算并返回结果
            byte[] mdBytes = md.digest();
            // 将字节数组转化为十六进制字符串并返回
            return new BigInteger(1, mdBytes).toString(16);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
